package com.forlove.app.manager;

import android.content.Context;
import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.utils.AppPreferenceHelper;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.RandomDataUtil;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.qselect.friend.entity.FriendEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.NewsFriendsStatus;
import com.forlove.app.qselect.login.entity.ContactsInfoEntity;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 好友相关的管理类
 * Created by yjl on 2017/10/24.
 */

public class FriendManager {
    public static String FirstSort = "⭐︎";

    /**
     * 保存好友列表
     *
     * @param context
     * @param friendList
     */
    public static void saveFriendList(Context context, String friendList) {
        AppPreferenceHelper.getInstance(context).putString(PreferenceKeys.MINE_FRIEND_LIST, friendList);
    }

    /**
     * 获取好友列表缓存
     *
     * @param context
     * @return
     */
    public static String getFriendList(Context context) {
        return AppPreferenceHelper.getInstance(context).getString(PreferenceKeys.MINE_FRIEND_LIST, "");
    }

    public static List<FriendEntity> getFriends(Context context) {
        FriendListEntity friendListEntity = new FriendListEntity();
        String friend = getFriendList(context);
        if (!TextUtils.isEmpty(friend)) {
            friendListEntity.parseJSONString(ParseHelper.getString(friend, "data"));
        }
        return friendListEntity.friendEntities;
    }


    /**
     * 保存最新的新好友的添加时间
     */
    public static void saveNewFriendAddTime(Context context, Long addtime) {
        AppPreferenceHelper.getInstance(context).putLong(PreferenceKeys.NEW_FRIEND_ADD, addtime);
    }

    public static long getNewFriendAddTime(Context context) {
        return AppPreferenceHelper.getInstance(context).getLong(PreferenceKeys.NEW_FRIEND_ADD, 0);
    }


    public static boolean showMineGuideView(Context context) {
        return AppPreferenceHelper.getInstance(context).getBoolean(PreferenceKeys.SHOW_MINE_CIRCLE_VIEW, true);
    }

    public static void saveMindeGuideView(Context context, boolean show) {
        AppPreferenceHelper.getInstance(context).putBoolean(PreferenceKeys.SHOW_MINE_CIRCLE_VIEW, show);
    }

    public static boolean showFriendGuideView(Context context) {
        return AppPreferenceHelper.getInstance(context).getBoolean(PreferenceKeys.SHOW_FRIEND_CIRCLE_VIEW, true);
    }

    public static void saveFriendGuideView(Context context, boolean show) {
        AppPreferenceHelper.getInstance(context).putBoolean(PreferenceKeys.SHOW_FRIEND_CIRCLE_VIEW, show);
    }

    public static void saveMineFriend(String mineFriend) {
        AppPreferenceHelper.getInstance(ForloveApplication.getInstance()).putString(PreferenceKeys.MINE_FRIEND_LIST_NEW, mineFriend);
    }

    public static String getMineFriend(Context context) {
        return AppPreferenceHelper.getInstance(context).getString(PreferenceKeys.MINE_FRIEND_LIST_NEW, "");

    }

    /**
     * 将通讯录好友转换成friendEntity
     * 并且将注册的好友的 排序改为 #
     * <p>
     * 会去掉通讯录多余好友
     *
     * @param friendEntities
     * @param contactsInfoEntities
     */
    public static List<MineFriendEntity> contactsToFriendEntity(List<MineFriendEntity> friendEntities,
                                                                List<ContactsInfoEntity> contactsInfoEntities) {
        if (friendEntities != null && contactsInfoEntities != null) {
            for (MineFriendEntity friendEntity : friendEntities) {
                for (int i = 0; i < contactsInfoEntities.size(); i++) {
                    ContactsInfoEntity contactsInfoEntity = contactsInfoEntities.get(i);
                    if (friendEntity.mobile.equals(contactsInfoEntity.getNumber())) {
                        friendEntity.copyContactEntity(contactsInfoEntity);
                        friendEntity.sortKey = "#";
                    }

                }
            }
        }
        return friendEntities;
    }


    /**
     * 将本地通讯录与返回的好友列表合并
     * 返回的是通讯录有的好友，会过滤通讯录没有的好友
     * <p>
     * 返回的是通讯录好友
     *
     * @param friendEntities
     * @param contactsInfoEntities
     * @return
     */
    public static List<MineFriendEntity> friendToContacts(List<MineFriendEntity> friendEntities,
                                                          List<ContactsInfoEntity> contactsInfoEntities) {
        List<MineFriendEntity> entities = new ArrayList<>();//已经注册通讯录好友
        List<ContactsInfoEntity> infoEntities = new ArrayList<>();
        infoEntities.addAll(contactsInfoEntities);
        //先将返回的注册好友包括进去
        for (MineFriendEntity friendEntity : friendEntities) {
            for (int i = 0; i < contactsInfoEntities.size(); i++) {
                ContactsInfoEntity contactsInfoEntity = contactsInfoEntities.get(i);
                if (friendEntity.mobile.equals(contactsInfoEntity.getNumber())) {
                    friendEntity.copyContactEntity(contactsInfoEntity);
                    friendEntity.sortKey = "⭐︎";
                    infoEntities.remove(contactsInfoEntity);
                    entities.add(friendEntity);
                }

            }

        }
        Collections.sort(entities);
        for (ContactsInfoEntity contactsInfoEntity : infoEntities) {
            MineFriendEntity friendEntity = new FriendEntity();
            //TODO 未设置姓名或者特殊字符显示为
            if (TextUtils.isEmpty(contactsInfoEntity.getSortKey())
                    || contactsInfoEntity.getSortKey().equals("#")) {
                contactsInfoEntity.setSortKey("#");
            }
            friendEntity.copyContactEntity(contactsInfoEntity);
            entities.add(friendEntity);
        }
        //再将通讯录的好友并进去
        return entities;
    }


    /**
     * 候选人至少三十个
     * <p>
     * 服务器推荐的少与30从通讯录补
     * <p>
     * *圈一圈好友和手机通讯录中合并获取30个,圈一圈优先
     */
    public synchronized static List<MineFriendEntity> getAllCandidateFriend(List<MineFriendEntity> recomandFriend,
                                                                            List<ContactsInfoEntity> contactsInfoEntities, int maxSize) {
        List<MineFriendEntity> entities = new ArrayList<>();//筛选后的好友列表
        if (recomandFriend != null) {
            entities.addAll(recomandFriend);
        }
        List<ContactsInfoEntity> commonList = new ArrayList<>();
        if (contactsInfoEntities != null) {
            commonList.addAll(contactsInfoEntities);
            int contactsInfoSize = contactsInfoEntities.size();
            //先将推荐的和通讯录相同的排除掉
            for (MineFriendEntity friendEntity : entities) {
                for (int i = 0; i < contactsInfoSize; i++) {
                    ContactsInfoEntity contactsInfoEntity = contactsInfoEntities.get(i);
                    //如果推荐的是同一个人就加进来
                    String mobile1 = "";
                    if (StringUtils.isNotEmpty(friendEntity.mobile)) {
                        mobile1 = friendEntity.mobile;
                    } else if (StringUtils.isNotEmpty(friendEntity.contactsNumber)) {
                        mobile1 = friendEntity.contactsNumber;
                    }
                    if (contactsInfoEntity != null && mobile1.equals(contactsInfoEntity.getNumber())) {
                        friendEntity.copyContactEntity(contactsInfoEntity);
                        commonList.remove(contactsInfoEntity);
                        break;
                    }
                }
            }
            int recomandLenth = entities.size();
            if (maxSize > 0 && recomandLenth < maxSize) {
                int size = maxSize - recomandLenth; //还需要添加的数目
                contactsInfoSize = commonList.size();
                if (contactsInfoSize <= size) {
                    //剩下的通讯录好友数目小与还需要添加的数目，全部加入到备选名单中
                    for (ContactsInfoEntity contactsInfoEntity : commonList) {
                        FriendEntity friendEntity = new FriendEntity(contactsInfoEntity);
                        entities.add(friendEntity);
                    }
                } else if (contactsInfoSize > size) {
                    int[] num = ContactsManager.randomArray(0, contactsInfoSize - 1, size);
                    for (int i = 0; i < num.length; i++) {
                        ContactsInfoEntity contactsInfoEntity = commonList.get(num[i]);
                        FriendEntity friendEntity = new FriendEntity(contactsInfoEntity);
                        entities.add(friendEntity);
                    }
                }
            } else if (maxSize < 0) {
                //全部加入
                for (ContactsInfoEntity contactsInfoEntity : commonList) {
                    FriendEntity friendEntity = new FriendEntity(contactsInfoEntity);
                    entities.add(friendEntity);
                }
            }

        }
        return entities;
    }

    /**
     * 合并两个好友列表
     *
     * @param recommendFriend
     * @param qyqFriend
     * @param size            小于0只是把两个列表合并。
     *                        大于0，则返回最多size个friend
     * @return
     */
    public static List<MineFriendEntity> mergeFriends(List<MineFriendEntity> recommendFriend, List<MineFriendEntity> qyqFriend, int size) {
        List<MineFriendEntity> mergeFriends = new ArrayList<>();
        if (recommendFriend == null || qyqFriend == null) {
            return recommendFriend == null ? qyqFriend : recommendFriend;
        }
        List<MineFriendEntity> mineFriendEntities = new ArrayList<>();
        mineFriendEntities.addAll(recommendFriend);
        if (recommendFriend.size() + qyqFriend.size() < size || size < 0) {
            mineFriendEntities.addAll(qyqFriend);
            mergeFriends.addAll(removeDuplicateFriend(mineFriendEntities));
        } else if (recommendFriend.size() > size) {
            return recommendFriend;
        } else if (recommendFriend.size() < size) {
            List<MineFriendEntity> entities = new ArrayList<>();
            entities.addAll(qyqFriend);
            for (MineFriendEntity recFriend : recommendFriend) {
                for (int i = 0; i < qyqFriend.size(); i++) {
                    MineFriendEntity friend = qyqFriend.get(i);
                    if (friend.mobile.equals(recFriend.mobile)) {
                        entities.remove(friend);
                        break;
                    }
                }
            }

            int deltSize = 30 - recommendFriend.size();
            mergeFriends.addAll(mineFriendEntities);
            //在去重后取出少的人数
            if (entities.size() > deltSize) {
                mergeFriends.addAll(RandomDataUtil.generateRandomDataNoRepeat(entities, deltSize));
            } else {
                mergeFriends.addAll(entities);
            }
        }
//        System.out.println("***********recommendFriend***********");
//        for (MineFriendEntity friendEntity : recommendFriend) {
//            System.out.println("friend mobile:" + friendEntity.mobile + "  realname:" + friendEntity.realname);
//        }
//        System.out.println("***********qyqFriend***********");
//        for (MineFriendEntity friendEntity : qyqFriend) {
//            System.out.println("friend mobile:" + friendEntity.mobile + "  realname:" + friendEntity.realname);
//        }
//        System.out.println("***********合并去重后***********");
//        for (MineFriendEntity friendEntity : mergeFriends) {
//            System.out.println("friend mobile:" + friendEntity.mobile + "  realname:" + friendEntity.realname);
//        }

        return mergeFriends;
    }

    /**
     * 根据手机号去重
     *
     * @param recommendFriend
     * @return
     */
    public static List<MineFriendEntity> removeDuplicateFriend(List<MineFriendEntity> recommendFriend) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                //字符串,则按照asicc码升序排列
                String mobile1 = friendEntity1.getAvaliableMobile();
                String mobile2 = friendEntity2.getAvaliableMobile();
                return mobile1.compareTo(mobile2);
            }
        });
        set.addAll(recommendFriend);
        return new ArrayList<>(set);
    }

    /**
     * 是否第一次打开圈一圈
     *
     * @param context
     * @param open
     */
    public static void saveFirstOpenCircle(Context context, boolean open) {
        AppPreferenceHelper.getInstance(context).putBoolean(PreferenceKeys.KEY_OPEN_FIRST_CIRCLE, open);
    }

    public static boolean getFirstOpenCircle(Context context) {
        return AppPreferenceHelper.getInstance(context).getBoolean(PreferenceKeys.KEY_OPEN_FIRST_CIRCLE, true);
    }


    /**
     * 获取答题过程中需要的筛选过后的30为候选人
     * 优先级：推荐候选人（设置好的候选人）》圈一圈好友》通讯录好友
     *
     * @param recommendFriend      推荐的好友
     * @param qyqFriend            圈一圈好友
     * @param contactsInfoEntities 通讯录好友
     * @param maxNum               大于0为设置合并后返回的人数，-1为只是去重合并列表
     * @return
     */
    public synchronized static List<MineFriendEntity> getSubjectCandidate(List<MineFriendEntity> recommendFriend,
                                                                          List<MineFriendEntity> qyqFriend,
                                                                          List<ContactsInfoEntity> contactsInfoEntities, int maxNum) {
        if (recommendFriend == null) {
            return recommendFriend;
        }
        List<MineFriendEntity> recommend = new ArrayList<>();
        recommend.addAll(recommendFriend);
        List<MineFriendEntity> mergFriend = new ArrayList<>();
        if (maxNum > 0) {
            if (recommend.size() >= maxNum) {
                mergFriend.addAll(recommend);
                return mergFriend;
            }
            //合并推荐候选人和圈一圈好友
            mergFriend.addAll(FriendManager.mergeFriends(recommend, qyqFriend, maxNum));
            if (mergFriend.size() < maxNum && contactsInfoEntities != null) {
                //人数还是少于30人，获取通讯录
                return FriendManager.getAllCandidateFriend(mergFriend, contactsInfoEntities, maxNum);
            }
        } else {
            List<MineFriendEntity> recomAndQyq = FriendManager.mergeFriends(recommend, qyqFriend, maxNum);
            List<MineFriendEntity> entities = FriendManager.getAllCandidateFriend(recomAndQyq, contactsInfoEntities, maxNum);
            mergFriend.addAll(entities);

        }
        return mergFriend;
    }


    /**
     * 如果是推荐好友排序考前
     * 会根据姓名排序，但是如果姓名相同但是手机号不同会重复，
     * 使用之前需要根据手机号去重
     *
     * @param mineFriendEntities
     * @return
     */
    public static List<MineFriendEntity> sortRecommend(List<MineFriendEntity> mineFriendEntities) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                //字符串,则按照是否是推荐好友排序
                if (friendEntity1.isRecomend == friendEntity2.isRecomend) {
                    Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
                    if (!friendEntity1.getAvaliableName().equals(friendEntity2.getAvaliableName())) {
                        //  如果一样姓名回去重
                        return cmp.compare(friendEntity1.getAvaliableName(), friendEntity2.getAvaliableName());
                    } else {
                        return cmp.compare(friendEntity1.getAvaliableMobile(), friendEntity2.getAvaliableMobile());
                    }
                }
                return friendEntity1.isRecomend ? -1 : 1;
            }
        });
        set.addAll(mineFriendEntities);
        return new ArrayList<>(set);
    }


    /**
     * 保存新的好友
     *
     * @param newFriendstr
     */
    public static void saveNewFriends(String newFriendstr) {
        AppPreferenceHelper.getInstance(ForloveApplication.getInstance())
                .putString(PreferenceKeys.KEY_NEW_FRIEND, newFriendstr);
    }


    public static List<MineFriendEntity> getNewFriends() {
        String strNewfriend = AppPreferenceHelper.getInstance(ForloveApplication.getInstance())
                .getString(PreferenceKeys.KEY_NEW_FRIEND, "");
        List<MineFriendEntity> mineFriendEntities = new ArrayList<>();
        if (StringUtils.isNotEmpty(strNewfriend)) {
            mineFriendEntities = ParseHelper.getModelList(strNewfriend, "data.friends", MineFriendEntity.class);
        }
        return mineFriendEntities;
    }

    /**
     * 获取未注册的通讯录好友
     *
     * @param contactsInfoEntities 本地通讯录
     * @param UnregisterFriend     未注册且有魅力值得本地通讯录好友
     * @param registerFriend       注册好友
     */
    public static List<MineFriendEntity> getUnregContactsFriends(List<ContactsInfoEntity> contactsInfoEntities,
                                                                 List<MineFriendEntity> UnregisterFriend,
                                                                 List<MineFriendEntity> registerFriend) {
        if (contactsInfoEntities == null) {
            return getCharmFriends(UnregisterFriend);
        }
        List<MineFriendEntity> commonFriends = new ArrayList<>();
        try {
            List<MineFriendEntity> unreg = new ArrayList<>();
            unreg.addAll(UnregisterFriend);
            List<ContactsInfoEntity> unregContacts = new ArrayList<>();
            unregContacts.addAll(contactsInfoEntities);
            //过滤掉本地注册通讯录好友
            for (ContactsInfoEntity contactsInfoEntity : contactsInfoEntities) {
                for (MineFriendEntity mineFriendEntity : registerFriend) {
                    if (mineFriendEntity.mobile.equals(contactsInfoEntity.getNumber())) {
                        unregContacts.remove(contactsInfoEntity);
                    }
                }
            }

            List<ContactsInfoEntity> noCharmsContacts = new ArrayList<>();
            noCharmsContacts.addAll(unregContacts); //没有魅力值和共同好友的通讯录联系人

            for (MineFriendEntity mineFriendEntity : unreg) {
                for (ContactsInfoEntity contactsInfoEntity : unregContacts) {
                    if (contactsInfoEntity.getNumber().equals(mineFriendEntity.mobile)) {
                        mineFriendEntity.copyContactEntity(contactsInfoEntity);
                        noCharmsContacts.remove(contactsInfoEntity);
                    }
                }
                mineFriendEntity.sortKey = "⭐︎";
            }

            commonFriends = new ArrayList<>();
            for (ContactsInfoEntity contactsInfoEntity : noCharmsContacts) {
                MineFriendEntity mineFriendEntity = new MineFriendEntity();
                mineFriendEntity.copyContactEntity(contactsInfoEntity);
                unreg.add(mineFriendEntity);
            }
            commonFriends.addAll(getCharmFriends(unreg));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commonFriends;
    }

    /**
     * 优先显示有共同好友的
     * 再显示魅力值
     * 最后通讯录姓名排序
     *
     * @param hasCharms
     * @return
     */
    public static List<MineFriendEntity> getCharmFriends(List<MineFriendEntity> hasCharms) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                //优先共同好友数
                //再魅力值数
                //再通讯录名称
                if (friendEntity1.commonFriends != friendEntity2.commonFriends) {
                    if (friendEntity1.commonFriends > friendEntity2.commonFriends) {
                        return -1;
                    } else {
                        return 1;
                    }
                } else if (friendEntity1.charm != friendEntity2.charm) {
                    if (friendEntity1.charm > friendEntity2.charm) {
                        return -1;
                    } else {
                        return 1;
                    }
                } else {
                    Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
                    if (StringUtils.isNotEmpty(friendEntity1.getContactsName())
                            && StringUtils.isNotEmpty(friendEntity2.getContactsName())
                            && !friendEntity1.getContactsName().equals(friendEntity2.getContactsName())) {
                        return cmp.compare(friendEntity1.getContactsName(), friendEntity2.getContactsName());
                    } else {
                        String mobile1 = friendEntity1.getAvaliableMobile();
                        String mobile2 = friendEntity2.getAvaliableMobile();
                        return mobile1.compareTo(mobile2);
                    }
                }
            }
        });
        set.addAll(hasCharms);
        return new ArrayList<>(set);
    }

    /**
     * 根据是否被圈选过排序
     *
     * @param circleFriend
     * @return
     */
    public static List<MineFriendEntity> sortCircleNum(List<MineFriendEntity> circleFriend) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                if (friendEntity1.hasCircle == friendEntity2.hasCircle) {
                    if (!friendEntity1.getAvaliableName().equals(friendEntity2.getAvaliableName())) {
                        Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
                        return cmp.compare(friendEntity1.getAvaliableName(), friendEntity2.getAvaliableName());
                    } else {
                        //字符串,则按照asicc码升序排列
                        String mobile1 = friendEntity1.getAvaliableMobile();
                        String mobile2 = friendEntity2.getAvaliableMobile();
                        return mobile1.compareTo(mobile2);
                    }
                }
                return friendEntity1.hasCircle ? -1 : 1;

            }
        });
        set.addAll(circleFriend);
        return new ArrayList<>(set);
    }

    /**
     * 按照是否被设置为候选人》注册》共同好友数》魅力值
     *
     * @param circleFriend
     * @return
     */
    public static synchronized List<MineFriendEntity> sortRegisterFriend(List<MineFriendEntity> circleFriend) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
                if (friendEntity1.isRecomend == friendEntity2.isRecomend) {
                    if (StringUtils.isNotEmpty(friendEntity1.realname)
                            && StringUtils.isNotEmpty(friendEntity2.realname)) {
                        if (friendEntity1.commonFriends == friendEntity2.commonFriends) {
                            if (friendEntity1.charm == friendEntity2.charm) {
                                //姓名
                                if (!friendEntity1.getAvaliableName().equals(friendEntity2.getAvaliableName())) {
                                    return cmp.compare(friendEntity1.getAvaliableName(), friendEntity2.getAvaliableName());
                                } else {
                                    return cmp.compare(friendEntity1.getAvaliableMobile(), friendEntity2.getAvaliableMobile());
                                }

                            } else {
                                //魅力值数
                                return friendEntity1.charm > friendEntity2.charm ? -1 : 1;
                            }
                        } else {
                            //共同好友数
                            return friendEntity1.commonFriends > friendEntity2.commonFriends ? -1 : 1;
                        }

                    } else {

                        if (friendEntity1 != null && friendEntity2 != null
                                && !StringUtils.isNotEmpty(friendEntity1.realname)
                                && !StringUtils.isNotEmpty(friendEntity2.realname)) {
                            //都未注册，按照姓名
                            if (!friendEntity1.realname.equals(friendEntity2.realname)) {
                                return cmp.compare(friendEntity1.getAvaliableName(), friendEntity2.getAvaliableName());
                            } else {
                                return -1;
                            }
                        } else {
                            //有一个注册，返回注册的
                            return StringUtils.isNotEmpty(friendEntity1.realname) ? -1 : 1;
                        }
                    }
                }

                return friendEntity1.isRecomend ? -1 : 1;

            }
        });
        set.addAll(circleFriend);
        return new ArrayList<>(set);
    }


    /**
     * 新的好友排序
     *
     * @param mineFriendEntities
     */
    public static synchronized List<MineFriendEntity> sortNewFriend(List<MineFriendEntity> mineFriendEntities) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                if (friendEntity1.status == friendEntity2.status) {
                    return friendEntity1.addtime >= friendEntity2.addtime ? -1 : 1;
                }
                if (friendEntity1.status == NewsFriendsStatus.UNADDED.getCode()) {
                    return -1;
                } else if (friendEntity1.status == NewsFriendsStatus.WAITING.getCode()) {
                    if (friendEntity2.status == NewsFriendsStatus.UNADDED.getCode()) {
                        return 1;
                    }
                    return -1;
                }

                return friendEntity1.status == NewsFriendsStatus.UNADDED.getCode() ? -1 : 1;

            }
        });
        set.addAll(mineFriendEntities);
        return new ArrayList<>(set);
    }


    /**
     * 好友请求排序
     *
     * @return
     */
    public static synchronized List<MineFriendEntity> sortFriendRequest(List<MineFriendEntity> mineFriendEntities) {
        Set<MineFriendEntity> set = new TreeSet<>(new Comparator<MineFriendEntity>() {
            @Override
            public int compare(MineFriendEntity friendEntity1, MineFriendEntity friendEntity2) {
                if (friendEntity1.status == friendEntity2.status) {
                    return friendEntity1.addtime >= friendEntity2.addtime ? -1 : 1;
                }
                if (friendEntity1.status == NewsFriendsStatus.UNCONFIRM.getCode()) {
                    return -1;
                } else if (friendEntity1.status == NewsFriendsStatus.CONFIRMED.getCode()) {
                    if (friendEntity2.status == NewsFriendsStatus.UNCONFIRM.getCode()) {
                        return 1;
                    }
                    return -1;
                }

                return friendEntity1.status == NewsFriendsStatus.UNCONFIRM.getCode() ? -1 : 1;

            }
        });
        set.addAll(mineFriendEntities);
        return new ArrayList<>(set);
    }


}
